package com.aaa.controller;

import com.aaa.entity.CourseEntity;
import com.aaa.entity.Message;
import com.aaa.entity.Pager;
import com.aaa.service.CourseService;
import com.aaa.service.CourseServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "CourseServlet",urlPatterns="/course.action")
public class CourseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null){
            switch (method){
                case "queryAll":
                    queryAll(request,response);
                    break;
                case "add":
                    add(request,response);
                    break;
                case "queryOne":
                    queryOne(request,response);
                    break;
                case "delete":
                    delete(request,response);
                    break;
                case "queryType":
                    queryType(request,response);
                    break;
                case "updateState":
                    updateState(request,response);
                    break;
                case "queryTeacher":
                    queryTeacher(request,response);
                    break;

            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }


    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sPageNo=request.getParameter("pageNo");
        if (sPageNo==null) sPageNo="1";
        int pageNo= Integer.parseInt(sPageNo);
        Map<String,String> map=new HashMap<>();
        map.put("course_name",request.getParameter("course_name"));
        map.put("minCharge",request.getParameter("minCharge"));
        map.put("maxCharge",request.getParameter("maxCharge"));
        map.put("status",request.getParameter("status"));
        map.put("user_id",request.getParameter("user_id"));
        map.put("type_id",request.getParameter("type_id"));
        map.put("minHour",request.getParameter("minHour"));
        map.put("maxHour",request.getParameter("maxHour"));
        CourseService service=new CourseServiceImpl();
        Pager pager= service.queryByPages(pageNo,map);
        String str= JSON.toJSONStringWithDateFormat(pager,"yyyy-MM-dd");
        response.getWriter().print(str);
    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String course_id=request.getParameter("course_id");
        int courseId=course_id==""?0: Integer.parseInt(course_id);
        String type_id=request.getParameter("type_id");
        String course_name=request.getParameter("course_name");
        String Charge=request.getParameter("charge");
        String Hour=request.getParameter("hour");
        String Teacher=request.getParameter("user_id");
        int teacher= Integer.parseInt(Teacher);



        CourseEntity course=new CourseEntity();
        course.setCourseId(courseId);
        course.setTypeId(Integer.valueOf(type_id));
        course.setCourseName(course_name);
        course.setCharge(new BigDecimal(Charge));
        course.setHour(Hour);

        Message message=new Message();

        CourseService service=new CourseServiceImpl();
        message=service.course_Update(course,teacher);
        String str= JSON.toJSONStringWithDateFormat(message,"yyyy-MM-dd");
        response.getWriter().print(str);

    }

    protected void queryOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String cid=request.getParameter("course_id");
       int course_id= Integer.parseInt(cid);
       CourseService service=new CourseServiceImpl();
       Map map=service.queryOne(course_id);
       String str= JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
       response.getWriter().print(str);
    }


    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid=request.getParameter("course_id");
        int course_id= Integer.parseInt(cid);
        CourseService service=new CourseServiceImpl();
        int result=service.delete(course_id);
        response.getWriter().print(result);
    }


    protected void queryType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       CourseService service=new CourseServiceImpl();
       List list=service.queryType();
       String str=JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
       response.getWriter().print(str);
    }

    protected void updateState(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid=request.getParameter("course_id");
        int course_id= Integer.parseInt(cid);
        String st=request.getParameter("state");
        int state= Integer.parseInt(st);
        CourseService service=new CourseServiceImpl();
        int result=service.updateState(state,course_id);
        response.getWriter().print(result);
    }

    protected void queryTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseService service=new CourseServiceImpl();
        List list=service.queryTeacher();
        String str=JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        response.getWriter().print(str);
    }
}
